package com.meetingshow.meeting.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.groups.Default;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.meeting.dao.model.TopicPO;
import com.meetingshow.meeting.service.ITopicService;
import com.meetingshow.meeting.service.model.TopicQueryParas;
import com.meetingshow.meeting.uiservice.model.TopicQueryParasVO;
import com.meetingshow.meeting.uiservice.model.TopicVO;
import com.meetingshow.utils.BaseUtil;

public class UITopicServiceImp extends BaseUIServiceImp implements
        IUITopicService {

    public final static Logger logger = Logger
            .getLogger(UITopicServiceImp.class);

    private ITopicService topicService;

    /**
     * @return the topicService
     */
    public ITopicService getTopicService() {
        return topicService;
    }

    /**
     * @param topicService
     *            the topicService to set
     */
    public void setTopicService(ITopicService topicService) {
        this.topicService = topicService;
    }

    @Override
    public Integer saveTopic(TopicVO topicVO)
            throws BaseUIServiceException {

        if (!BaseUtil.objectNotNull(topicVO)) {
            this.throwValidaterException("topicVO is null.");
        }

        topicVO.setCreateTime(new Date());

        validateThrowEx(topicVO);

        TopicQueryParas topicQueryParas = new TopicQueryParas();
        topicQueryParas.setTopicName_e(topicVO.getTopicName());

        QueryResults<TopicPO> mtQueryResults = null;

        try {
            mtQueryResults = getTopicService().queryTopic(
                    topicQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }

        if (QueryResults.resultsListNotNull(mtQueryResults)) {
            logger.error("topicName [ " + topicVO.getTopicName()
                    + " ] exists.");
            throw new BaseUIServiceException("topicName [ "
                    + topicVO.getTopicName() + " ] exists.");
        }

        try {
            Integer topicId = getTopicService().saveTopic(
                    topicVO.toTopicPO());
            topicVO.setTopicId(topicId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }

        return topicVO.getTopicId();
    }

    @Override
    public void updateTopic(TopicVO topicVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(topicVO)) {
            this.throwValidaterException("topicVO is null.");
        }

        validateThrowEx(topicVO, Default.class, UpdateValidator.class);

        TopicQueryParas topicQueryParas = new TopicQueryParas();
        topicQueryParas.setTopicId_e(String.valueOf(topicVO
                .getTopicId()));

        QueryResults<TopicPO> topicQueryResults = null;
        try {
            topicQueryResults = getTopicService().queryTopic(
                    topicQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }

        if (QueryResults.resultsListNotNull(topicQueryResults)) {
            TopicPO persistTopicPO = topicQueryResults
                    .getResultList().get(0);
            persistTopicPO.setTopicName(topicVO.getTopicName());
            persistTopicPO.setTopicDesc(topicVO.getTopicDesc());

            // 验证typeName是否重复
            topicQueryParas = new TopicQueryParas();
            topicQueryParas.setTopicName_e(topicVO.getTopicName());

            topicQueryResults = null;
            try {
                topicQueryResults = getTopicService()
                        .queryTopic(topicQueryParas);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }

            if (QueryResults.resultsListNotNull(topicQueryResults)
                    && !topicVO.getTopicId().equals(
                            topicQueryResults.getResultList().get(0)
                                    .getTopicId())) {
                logger.error("topicName [ " + topicVO.getTopicName()
                        + " ] exists.");
                throw new BaseUIServiceException("topicName [ "
                        + topicVO.getTopicName() + " ] exists.");
            }

            try {
                getTopicService().updateTopic(persistTopicPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public void removeTopic(TopicVO topicVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(topicVO)) {
            this.throwValidaterException("topicVO is null.");
        }

        validateThrowEx(topicVO, RemoveValidator.class);

        TopicQueryParas topicQueryParas = new TopicQueryParas();
        topicQueryParas.setTopicId_e(String.valueOf(topicVO
                .getTopicId()));

        QueryResults<TopicPO> topicQueryResults = null;
        try {
            topicQueryResults = getTopicService().queryTopic(
                    topicQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }

        if (QueryResults.resultsListNotNull(topicQueryResults)) {
            TopicPO persistTopicPO = topicQueryResults
                    .getResultList().get(0);
            try {
                getTopicService().removeTopic(persistTopicPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public TopicVO loadTopicById(Integer topicId) throws BaseUIServiceException {
        if (topicId == null)
            this.throwValidaterException("topicId is null.");
        
        TopicPO topicPO = null;
        try {
            topicPO = getTopicService().loadTopicById(topicId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (topicPO == null)
            return null;
        else
            return new TopicVO(topicPO);
    }
    
    @Override
    public QueryResults<TopicVO> queryTopic(
            TopicQueryParasVO topicQueryParasVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(topicQueryParasVO)) {
            this.throwValidaterException("topicQueryParasVO is null.");
        }

        validateThrowEx(topicQueryParasVO);

        QueryResults<TopicPO> topicPOQueryResults = null;
        try {
            topicPOQueryResults = getTopicService()
                    .queryTopic(
                            topicQueryParasVO.toTopicQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }

        QueryResults<TopicVO> topicVOQueryResults = new QueryResults<TopicVO>();
        List<TopicVO> topicVOList = new ArrayList<TopicVO>();
        if (QueryResults.resultsListNotNull(topicPOQueryResults)) {
            for (TopicPO topicPO : topicPOQueryResults
                    .getResultList()) {
                if (BaseUtil.objectNotNull(topicPO)) {
                    topicVOList.add(new TopicVO(topicPO));
                }
            }
        }

        topicVOQueryResults.setFirstResultNo(topicPOQueryResults
                .getFirstResultNo());
        topicVOQueryResults.setResultsPerGroup(topicPOQueryResults
                .getResultsPerGroup());
        topicVOQueryResults.setResultList(topicVOList);
        topicVOQueryResults.setTotalResultsNum(topicPOQueryResults
                .getTotalResultsNum());

        return topicVOQueryResults;
    }

}
